package com.rookie.im.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rookie.im.common.exception.BusinessException;
import com.rookie.im.user.domain.entity.User;
import com.rookie.im.user.mapper.UserMapper;
import com.rookie.im.user.req.ImportUserReq;
import com.rookie.im.user.resp.ImportUserResp;
import com.rookie.im.user.service.IUserService;
import com.rookie.im.user.service.adapter.UserAdapter;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shadaileng
 * @since 2024-05-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    static final int MAX_IMPORT_USER_COUNT = 100;
    @Override
    public User getUserById(int id) {
        return lambdaQuery().eq(User::getId, id).one();
    }

    @Override
    public ImportUserResp importUser(ImportUserReq req) {
        if (req.getUsers().size() > MAX_IMPORT_USER_COUNT) {
            throw new BusinessException("导入用户数量超过限制");
        }
        req.getUsers().forEach(user -> {
            save(UserAdapter.importUser(user));
        });
        return null;
    }
}
